﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Grain.Crm.DataMigrator.CrmServices;

namespace Grain.Crm.DataMigrator.Imports
{
    public class EducationImport : ImportBase
    {
        public EducationImport(CrmService service, WhoAmIResponse whoAmIresponse, ImportPayload importPayload)
            : base(service, whoAmIresponse, importPayload)
        {
        }

        protected override void Preprocess(out bool entityExists)
        {
            var query = new QueryByAttribute();
            query.EntityName = ThisEntityName;

            var schoolId = ImportsHelper.GetEntityId(_service, EntityName.new_school.ToString(),
                                                     new[] {"new_name"}, new[] { FileContent.GetByHeading(HeaderRow,"School") });

            var facultyId = ImportsHelper.GetEntityId(_service, EntityName.new_faculty.ToString(),
                                                     new[] { "new_name" }, new[] { FileContent.GetByHeading(HeaderRow, "Faculty") });

            var specializationId = ImportsHelper.GetEntityId(_service, EntityName.new_specialization.ToString(),
                                                     new[] { "new_name" }, new[] { FileContent.GetByHeading(HeaderRow, "Specialization") });


            if (schoolId == null)
            {
                ConsoleLog.Warning("Referenced School was not found. Ignoring it. School: " + FileContent.GetByHeading(HeaderRow, "School"));
                FileContent = FileContent.RemoveByHeading(HeaderRow, "School");
            }

            if (facultyId == null)
            {
                ConsoleLog.Warning("Referenced Faculty was not found. Ignoring it. Faculty: " + FileContent.GetByHeading(HeaderRow, "Faculty"));
                FileContent = FileContent.RemoveByHeading(HeaderRow, "Faculty");
            }

            if (specializationId == null)
            {
                ConsoleLog.Warning("Referenced Specialization was not found. Ignoring it. Specialization: " + FileContent.GetByHeading(HeaderRow, "Specialization"));
                FileContent = FileContent.RemoveByHeading(HeaderRow, "Specialization");
            }


            var educationKeyId = ImportsHelper.GetEntityId(_service, 
                EntityName.new_education.ToString(), new string[] { "new_name", "new_contactid", "new_languageid", "new_curriculumvitaeid" },
                new string[] { FileContent.GetByHeading(HeaderRow, "Name"), ImportsGlobal.ContactId, ImportsGlobal.LanguageId, ImportsGlobal.CVId });
            
            HeaderRow += ", Contact, Curriculum Vitae, Language";
            FileContent += "," + ImportsGlobal.ContactId + "," + ImportsGlobal.CV + "," + ImportsGlobal.Language;

            if (educationKeyId != null)
            {
                entityExists = true;

                ImportsHelper.AddKeyValueToContent(educationKeyId.GetValueOrDefault(), ThisEntityName, ref HeaderRow, ref FileContent);
            }
            else entityExists = false;
        }

        protected override void Postprocess()
        {
            
        }
    }
}
